package com.ijmacd.footballtracker;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.text.format.Time;
import android.util.Log;
import android.widget.RemoteViews;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Define a simple widget that shows the Wiktionary "Word of the day." To build
 * an update we spawn a background {@link Service} to perform the API queries.
 */
public class MatchWidget extends AppWidgetProvider {
	static final String TAG = "MatchWidget";
	static final String TEAM_UUID = "ef49f0d0-5180-3b3e-516f-bd6a7d68ad17";
	private static final String[] PROJECTION = new String[] {
        Provider.Matches._ID, // 0
        Provider.Matches.DATE, // 1
        Provider.Matches.STATUS, // 2
        Provider.Matches.HOME_TEAM_NAME, // 3
        Provider.Matches.AWAY_TEAM_NAME, // 4
        Provider.Matches.HOME_GOALS, // 5
        Provider.Matches.AWAY_GOALS, // 6
	};
    private static final int COLUMN_INDEX_UUID = 0;
    private static final int COLUMN_INDEX_DATE = 1;
    private static final int COLUMN_INDEX_STATUS = 2;
    private static final int COLUMN_INDEX_HOME_NAME = 3;
    private static final int COLUMN_INDEX_AWAY_NAME = 4;
    private static final int COLUMN_INDEX_HOME_GOALS = 5;
    private static final int COLUMN_INDEX_AWAY_GOALS = 6;
    
    private ArrayList<Integer> mWidgets = new ArrayList<Integer>();
	
	@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_match);
    	Uri u = Uri.parse(Provider.Matches.CONTENT_URI + "?team=" + TEAM_UUID);
    	Intent intent;
    	PendingIntent pending;
        Cursor c = context.getContentResolver().query(u, PROJECTION, null, null, null);
        for(int i = 0; i < appWidgetIds.length; i++){
        	if(!mWidgets.contains(appWidgetIds[i])) mWidgets.add(appWidgetIds[i]);
        	int pos = mWidgets.indexOf(appWidgetIds[i]);
	        if(c.moveToPosition(pos)){
	        	Log.d(TAG, "Update widget(" + i + ") " + appWidgetIds[i]);
	        	u = Uri.withAppendedPath(Provider.Matches.CONTENT_URI, c.getString(COLUMN_INDEX_UUID));
	        	intent  = new Intent(Intent.ACTION_VIEW, u);
	        	pending = PendingIntent.getActivity(context, 0, intent, 0);
	        	views.setOnClickPendingIntent(R.id.widget_view, pending);
	        	views.setTextViewText(R.id.home_team_txt, c.getString(COLUMN_INDEX_HOME_NAME));
	        	views.setTextViewText(R.id.home_goals_txt, c.getString(COLUMN_INDEX_HOME_GOALS));
	        	views.setTextViewText(R.id.away_team_txt, c.getString(COLUMN_INDEX_AWAY_NAME));
	        	views.setTextViewText(R.id.away_goals_txt, c.getString(COLUMN_INDEX_AWAY_GOALS));
	        	appWidgetManager.updateAppWidget(appWidgetIds[i], views);
	        }else break;
    	}
        c.close();
    }
	
    @Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
	}
}
